package com.app.controller.screen;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.app.mapper.*;
import com.app.model.ConfigModel;
import com.app.model.CustomerModel;
import com.app.model.UserModel;
import com.app.model.WorkApplyModel;
import com.app.service.ConfigService;
import com.app.service.CustomerService;
import com.app.util.PageUtil;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 平台操作控制器
 *
 * @author yoominic
 * @date 2023/10/19
 */
@RestController
public class PlatformOperationController {


    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ConfigService configService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JobPublishMapper jobPublishMapper;
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private BrowseNumberMapper browseNumberMapper;
    @Autowired
    private WorkApplyMapper workApplyMapper;
    @Autowired
    private OperationRecordsMapper operationRecordsMapper;
    @Autowired
    private OpinionMapper opinionMapper;


    /**
     * 每个乡镇街道注册登记人数
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/regPeoTownStreet")
    private Result<Object> numberRegisteredPeopleInEachTownshipStreet()
    {
        return  Result.success(this.customerService.regPeoTownStreet());
    }



    /**
     * 每个乡镇街道访问人数
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/browserPeoTownStreet")
    private Result<Object> browserPeoTownStreet()
    {
        return  Result.success(this.customerService.browserPeoTownStreet());
    }


    /**
     * 查询街道信息
     *
     * @param city 城市
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/queryStreetInfo")
    private Result<Object> queryStreetInfo(@RequestParam(value = "city") String city)
    {
        Map<String,Object> responseMap=new HashMap<>();
        responseMap.put("regCount",this.userMapper.queryRegCount(city));
        responseMap.put("browseCount",this.browseNumberMapper.queryBrowseCount(city));
        responseMap.put("companyCount",this.enterpriseMapper.queryCompanyCount(city));
        responseMap.put("positionCount",this.jobPublishMapper.queryPositionCount(city));
        return  Result.success(responseMap);
    }

    /**
     * 运营数据
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/operationalData")
    private Result<Object> operationalData()
    {
        Map<String,Object> operationalData=new HashMap<>();
        operationalData.put("allReg",this.userMapper.selectCount(null));
        operationalData.put("allBrowse", this.browseNumberMapper.selectCount(null));
        operationalData.put("allCompany", this.enterpriseMapper.selectCount(null));
        operationalData.put("allJob", this.jobPublishMapper.selectCount(null));
        operationalData.put("allSuccess", this.workApplyMapper.selectCount(null));
        return Result.success(operationalData);
    }


    /**
     * 访问趋势数据
     * 24小时accessTrends
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/accessTrendsData")
    private Result<Object> accessTrendsData() {
        List<Map<String, Object>> responseList = new ArrayList<>();
        List<ConfigModel> staticsTown = this.configService.getStaticsTownId();
        for (ConfigModel s : staticsTown
        ) {

            String time = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
            List<Map<String, Object>> accessTrendsData = this.browseNumberMapper.accessTrendsData(s.getId(), time);
            responseList.add(handleAccessTrendsData(accessTrendsData, s.getName()));
        }
        return Result.success(responseList);
    }


    /**
     * 处理访问趋势数据
     *
     * @param accessTrendsData 访问趋势数据
     * @param name             姓名
     * @return {@link Map}<{@link String},{@link Object}>
     */
    public Map<String, Object> handleAccessTrendsData(List<Map<String, Object>> accessTrendsData, String name) {
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("name", name);

        List<Long> count = new ArrayList<>();
        accessTrendsData.stream().forEach(s -> {
            count.add((Long) s.get("count"));
        });
        responseMap.put("data", count);

        return responseMap;

    }


    /**
     * 用户频率分析
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/userFrequencyAnalysis")
    private Result<Object> userFrequencyAnalysis() {
        Map<String, Object> operationalData = new HashMap<>();
        Date startTime = new Date();
        operationalData.put("today", this.browseNumberMapper.userFrequencyAnalysis(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())));
        Date threeDayEndTime = DateUtil.offsetDay(startTime, -3);
        operationalData.put("threeDay", this.browseNumberMapper.userFrequencyAnalysis(threeDayEndTime, startTime));
        Date sevenDayEndTime = DateUtil.offsetDay(startTime, -7);
        operationalData.put("sevenDay", this.browseNumberMapper.userFrequencyAnalysis(sevenDayEndTime, startTime));
        Date halfAMonthEndTime = DateUtil.offsetDay(startTime, -15);
        operationalData.put("halfAMonth", this.browseNumberMapper.userFrequencyAnalysis(halfAMonthEndTime, startTime));
        Date monthEndTime = DateUtil.offsetDay(startTime, -30);
        operationalData.put("month", this.browseNumberMapper.userFrequencyAnalysis(monthEndTime, startTime));
        return Result.success(operationalData);
    }


    /**
     * 用户频率数据
     *
     * @param city 城市
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/userFrequencyData")
    private Result<Object> userFrequencyData(@RequestParam(value = "city", required = false) String city) {
        Map<String, Integer> responseMap = new HashMap<>();


        responseMap.put("visit", this.browseNumberMapper.visitByCity(city));
        responseMap.put("forwarding", this.operationRecordsMapper.forwarding(city));
        return Result.success(responseMap);
    }


    /**
     * 反馈
     *
     * @return {@link Result}
     */
    @GetMapping("/feedback")
    public Result feedback() {

        List<Map<String, Integer>> responseList = this.opinionMapper.feedback();

        return Result.success(responseList);
    }


    @GetMapping("/matchSuccess")
    public Result matchSuccess(Integer page, Integer limit) {

        QueryWrapper<WorkApplyModel> workApplyModelQueryWrapper = new QueryWrapper<>();
        workApplyModelQueryWrapper.eq("is_hire", 1);
        List<WorkApplyModel> workApplyModelList = this.workApplyMapper.selectList(workApplyModelQueryWrapper);

        List<CustomerModel> customerModels = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(workApplyModelList)) {
            for (WorkApplyModel model : workApplyModelList
            ) {
                CustomerModel customerModel = this.customerMapper.selectById(model.getCustomerId());
                if (customerModel != null) {
                    if (customerModel.getSex() != null) {
                        customerModel.setSexText(customerModel.getSex() == 1 ? "男" : "女");
                    }
                    if (customerModel.getRealName() != null) {
                        customerModel.setRealName(DesensitizedUtil.chineseName(customerModel.getRealName()));
                    }
                    if (customerModel.getBirthday() != null) {
                        customerModel.setBirthdayText(customerModel.getBirthday());
                        customerModel.setAge((int) DateUtil.age(DateUtil.parseDate(customerModel.getBirthday()), DateUtil.date()));
                        ;
                    }
                    try {
                        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("c_id", customerModel.getId());
                        UserModel userModel = this.userMapper.selectOne(queryWrapper);
                        customerModel.setMobile(DesensitizedUtil.mobilePhone(userModel.getMobile()));
                    } catch (Exception e) {

                    }


                }

                customerModels.add(customerModel);

            }
        }

        return Result.success(new PageUtil<CustomerModel>(page, limit, customerModels));
    }



}
